package com.hzhr.iot.device.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuery;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.DeviceOptions;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.constant.UserOptions;
import com.hzhr.iot.device.convertor.custom.IotDeviceCustomConvertor;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.entity.ext.IotDeviceEntityExt;
import com.hzhr.iot.device.model.pressureMonitor_code.PressureMonitor_CodeGetModel;
import com.hzhr.iot.device.model.pressureMonitor_code.PressureMonitor_CodePutModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import com.hzhr.iot.device.service.helper.CollectDeviceServiceHelper;
import com.hzhr.iot.device.service.helper.CollectDevice_CodeServiceHelper;
import com.hzhr.iot.device.service.helper.User_CompanyServiceHelper;
import com.hzhr.iot.device.service.helper.User_DeptServiceHelper;
import com.hzhr.iot.user.sdk.service.User_CompanyService;
import com.hzhr.iot.user.sdk.service.User_DeptService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@CygService(entity = IotDeviceEntity.class)
@CygQuerys({
        @CygQuery(name = "_equal_id", query = "equal_id", type = Integer.class),
})
public class PressureMonitor_CodeService extends BaseService {
    @Autowired
    IotDeviceCustomConvertor iotDeviceCustomConvertor;
    @Autowired
    IotDeviceDao iotDeviceDao;
    @Autowired
    IotDeviceService iotDeviceService;
    @Autowired
    User_CompanyService user_companyService;
    @Autowired
    User_DeptService user_deptService;
    @Autowired
    User_DeptServiceHelper user_deptServiceHelper;
    @Autowired
    DeviceOptions deviceOptions;
    @Autowired
    CollectDeviceServiceHelper collectDeviceServiceHelper;
    @Autowired
    User_CompanyServiceHelper user_companyServiceHelper;
    @Autowired
    UserOptions userOptions;
    @Autowired
    CollectDevice_CodeServiceHelper collectDevice_codeServiceHelper;
    @Autowired
    PressureMonitor_CodeService pressureMonitor_codeService;

    //region javadoc注释 : putByPressureMonitorId
    /**
     * 更新压力监测仪的设备代码 <br/><br/>
     * 简介 : 更新压力监测仪的设备代码 <br/>
     * 步骤 : <br/>
     * 1. 查询压力监测仪 : (√) <br/>
     *  查询物联网设备(IOT_DEVICE)获取iotDevice_1，查询规则如下： <br/>
     *  (1) iotDevice_1.id = 路径参数pressureMonitorId； <br/>
     *  如果iotDevice_1不存在，抛出异常201； <br/>
     * 2. 验证新的设备代码的合法性 : (√) <br/>
     *  如果存在某个物联网设备(IOT_DEVICE)iotDeivce满足以下条件则抛出异常202： <br/>
     *  (1) iotDeivce.code = body.code； <br/>
     * 3. 更新压力监测仪信息 : (√) <br/>
     *  根据body更新iotDevice_1的相关信息； <br/>
     *  查询采集设备(COLLECT_DEVICE)获取collectDevice_1， <br/>
     *  如果collectDevice_1存在，更新collectDevice_1，更新规则如下： <br/>
     *  (1) collectDevice_1.code = body.code； <br/>
     * 4. 返回信息 : (√) <br/>
     *  调用“查询压力监测仪的设备代码”服务(device-service: /pressure-monitors/codes GET)获取result，调用规则如下： <br/>
     *  (1) accessMode: GLOBAL； <br/>
     *  (2) query: _equal_id = iotDevice_1.id； <br/>
     *  设置result属性，设置规则如下： <br/>
     *  (1) result.changeCount = 更新数量； <br/>
     *  返回result； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_info : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示详情;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitor_CodePutModel body <br/>
     * @see PressureMonitor_CodeGetModel result <br/>
     */ 
    //endregion
    //region 说明 : putByPressureMonitorId
    //更新压力监测仪的设备代码
    //简介 : 更新压力监测仪的设备代码
    //步骤 :
    //1. 查询压力监测仪 : (√)
    // 查询物联网设备(IOT_DEVICE)获取iotDevice_1，查询规则如下：
    // (1) iotDevice_1.id = 路径参数pressureMonitorId；
    // 如果iotDevice_1不存在，抛出异常201；
    //2. 验证新的设备代码的合法性 : (√)
    // 如果存在某个物联网设备(IOT_DEVICE)iotDeivce满足以下条件则抛出异常202：
    // (1) iotDeivce.code = body.code；
    //3. 更新压力监测仪信息 : (√)
    // 根据body更新iotDevice_1的相关信息；
    // 查询采集设备(COLLECT_DEVICE)获取collectDevice_1，
    // 如果collectDevice_1存在，更新collectDevice_1，更新规则如下：
    // (1) collectDevice_1.code = body.code；
    //4. 返回信息 : (√)
    // 调用“查询压力监测仪的设备代码”服务(device-service: /pressure-monitors/codes GET)获取result，调用规则如下：
    // (1) accessMode: GLOBAL；
    // (2) query: _equal_id = iotDevice_1.id；
    // 设置result属性，设置规则如下：
    // (1) result.changeCount = 更新数量；
    // 返回result；
    //endregion
    //region 参数 : putByPressureMonitorId
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),

    })
    //endregion
    //region 请求 : putByPressureMonitorId
    @RequestMapping(value = "/pressure-monitors/{pressureMonitorId}/codes", method = RequestMethod.PUT)
    //endregion
    public PressureMonitor_CodeGetModel putByPressureMonitorId(Map<String, String> header, Integer pressureMonitorId,
                                                         Map<String, Object> query, PressureMonitor_CodePutModel body) throws ServiceException {
        /* 查询压力监测仪 */
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(pressureMonitorId);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        queryModel.setEqual_categoryCode(deviceOptions.getCode("IOT_DEVICE_CATEGORY", "GAS_PRESSURE_MONITOR"));
        queryModel.setShow(true);
        IotDeviceEntity oriIotDevice = iotDeviceDao.selectEntity(queryModel);
        if (oriIotDevice == null) {//如果压力监测仪物联网设备不存在，则报错
            throw new ServiceException(Device_ErrorsEnum.IOTDEVICESERVICE_PUT_204);
        }

        /* 验证新的设备代码的合法性 */
        String oriIotDeviceCode = oriIotDevice.getCode();
        String code = body.getCode();
        if (oriIotDeviceCode == null || !oriIotDeviceCode.equals(code)) {
            queryModel = new IotDeviceCustomQueryModel();
            queryModel.setEqual_code(code);
            queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
            int count = iotDeviceDao.selectCount(queryModel);
            if (count > 0) {//如果更新后的物联网设备代码已存在，则报错
                throw new ServiceException(Device_ErrorsEnum.IOTDEVICESERVICE_PUT_204);
            }
        }

        /* 更新压力监测仪信息 */
        int loginUserUserId = getUserId(header);
        oriIotDevice.setCode(code);
        oriIotDevice.setUpdateTime(new Date());
        oriIotDevice.setUpdateUserId(loginUserUserId);
        int changeCount = iotDeviceDao.update(oriIotDevice);
        //如果采集设备存在，则更新压力监测仪采集设备代码
        Integer collectDeviceId = collectDeviceServiceHelper
                .getIdByIotDeviceId(initHeader(header, AccessModeEnum.GLOBAL), pressureMonitorId);
        if (collectDeviceId != null) {
            collectDevice_codeServiceHelper.putCodeByCollectDeviceId(
                    initHeader(header, AccessModeEnum.GLOBAL), collectDeviceId, code);
        }

        /* 返回信息 */
        query.put("_equal_id", oriIotDevice.getId());
        PressureMonitor_CodeGetModel result = pressureMonitor_codeService.get(initHeader(header, AccessModeEnum.GLOBAL), query);
        result.setChangeCount(changeCount);
        return result;
    }


    //region javadoc注释 : get
    /**
     * 查询压力监测仪的设备代码 <br/><br/>
     * 简介 : 查询压力监测仪的设备代码 <br/>
     * 步骤 : <br/>
     * 1. 查询采集设备 : (√) <br/>
     *  根据条件参数查询采集设备(IOT_DEVICE)； <br/>
     * 2. 返回信息 : (√) <br/>
     *  根据条件参数和查询结果组装响应并返回； <br/>
     * @param header <br/><br/>
     * @param query <br/><br/>
     * @throws ServiceException <br/>
     * @see PressureMonitor_CodeGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //查询压力监测仪的设备代码
    //简介 : 查询压力监测仪的设备代码
    //步骤 :
    //1. 查询采集设备 : (√)
    // 根据条件参数查询采集设备(IOT_DEVICE)；
    //2. 返回信息 : (√)
    // 根据条件参数和查询结果组装响应并返回；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "is_paging", value = "是否分页", defaultValue = "false",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "equal_id", value = "筛选条件 : where pressureMonitor_code.id = #{equal_id}",required = false, dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_show_info", value = "是否展示详情", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_is_paging", value = "是否分页", defaultValue = "false",required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_equal_id", value = "筛选条件 : where pressureMonitor_code.id = #{_equal_id}",required = false, dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/pressure-monitors/codes", method = RequestMethod.GET)
    //endregion
    public PressureMonitor_CodeGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询压力监测仪的设备代码 */
        //生成查询模型
        IotDeviceCustomQueryModel queryModel = iotDeviceCustomConvertor.pressureMonitor_CodeService_query2queryModel(query);
        queryModel.setEqual_isValid(CommonOptions.YES_NO.YES);
        //查询实体
        List<IotDeviceEntityExt> extList = null;
        String show = HzhrTypeUtil.object2string(query.get("_show"));
        if (!StringUtils.isEmpty(show)) {
            extList = iotDeviceDao.selectExtList(queryModel);
        }
        //查询数量
        Integer total = null;
        Boolean showTotal = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        if (showTotal != null && showTotal) {
            total = iotDeviceDao.selectCount(queryModel);
        }

        /* 返回结果 */
        PressureMonitor_CodeGetModel result = iotDeviceCustomConvertor.extList2pressureMonitor_CodeGetModel(extList, total, null, query);
        return result;
    }
}

